﻿using Bigger.Framework;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace Bigger.Study.ConsoleApp.Async
{
    public class GrammarAsyncExample : IExample
    {
        public async void Execute()
        {
            ConsoleHelper.WriteLineYellow("DoWorkAsync代码行开始执行");
            DoWorkAsync();
            //long content = await DoWorkAsync();
            ConsoleHelper.WriteLineYellow("DoWorkAsync代码行结束执行");
        }

        public async Task<long> DoWorkAsync()
        {
            MemoryStream content = new MemoryStream();

            HttpWebRequest webRequest = WebRequest.Create("http://msdn.microsoft.com/zh-cn/") as HttpWebRequest;
            if (webRequest != null)
            {
                // 返回回复结果
                using (WebResponse response = await webRequest.GetResponseAsync())
                {
                    using (Stream responseStream = response.GetResponseStream())
                    {
                        await responseStream.CopyToAsync(content);
                    }
                }
            }

            ConsoleHelper.WriteLineYellow("=============");

            return content.Length;
        }
    }
}
